package com.mlegrand.firestarter.util.write
{
	import com.mlegrand.firestarter.model.ApplicationModel;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class WriteProjectFiles
	{
		
		protected var _location:String;
		
		protected var _projectName:String;
		protected var _clientName:String;
		
		[Bindable]
		/**
		 * Singleton model reference.
		 */ 
		protected var model:ApplicationModel=ApplicationModel.getInstance();
		
		
		public function WriteProjectFiles(location:String, projectName:String, clientName:String)
		{
			_location = location
			_projectName = projectName;
			_clientName = clientName
		}
		
		
		public function write():void
		{
			writeMainDotMXML();
	 		/* 
	 		writeASProperties();
			writeFlexProperties();
			writeProjectFile();  
			writeSettingsPref();
			*/
			
			writeStyleSheet();
			writeMainAppXML();
			//writeBinSWF();
			writeBinXML();
			addLibsFolderAndDegrafa();
			addCairngorm();
		}
		
		
		protected function writeMainDotMXML():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/main.mxml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				while(t.charAt(0)!= '<')
				{
					t = t.slice(1, t.length);
				}
				
				var replacementString:String =  '.'+_clientName.toLowerCase()+'.'+_projectName.toLowerCase()+'.';
				
				t = t.replace('..', replacementString);
				t = t.replace('..', replacementString);
				t = t.replace('..', replacementString);
				t = t.replace('..', replacementString);								
				t = t.replace('com//assets/style.css', ('com/'+_clientName.toLowerCase()+'/'+_projectName.toLowerCase()+'/assets/style.css'));
				if(!model.projectBuilderManager.isAnAirApp)
				{
					t = t.replace('<mx:WindowedApplication', '<mx:Application');
					t = t.replace('</mx:WindowedApplication>', '</mx:Application>');
					t = t.replace('height="800" width="1200"', '');
				}
								
				f.writeUTFBytes(t);
				f.close();				
			}
			
			myTextLoader.load(new URLRequest("docs/WindowedApplicaiton.txt"));
		}

		protected function writeStyleSheet():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				t = t.replace('FIRESTARTER', _projectName.toUpperCase());
				
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+
					'/src/com/'+_clientName.toLowerCase()+
					'/'+_projectName.toLowerCase()+'/assets/style.css');
					
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/com/Style.txt"));
		}


		protected function writeSettingsPref():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/.settings/org.eclipse.core.resources.prefs');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/org.eclipse.core.resources.prefs.txt"));
		}

		protected function writeMainAppXML():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/src/main-app.xml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				var i:int = t.indexOf('<id></id>');
				t = t.slice(0, i+4)+'com.'+_projectName+t.slice(i+4, t.length);
				var j:int = t.indexOf('<name></name>');
				t = t.slice(0, j+4)+'com.'+_projectName+t.slice(j+4, t.length);
				trace('App XML: '+t);	
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/main_app.txt"));
		}

		protected function addLibsFolderAndDegrafa():void
		{
			var file:File = File.applicationDirectory.resolvePath(_location+ '/'+_projectName+'/libs/read_me.txt');			
			var f:FileStream = new FileStream();
			f.open(file, FileMode.WRITE);
			f.writeUTFBytes("Degrafa is an open source declarative graphics framework for Flex used by some of the industry's top designers and developers.\n " + 
					"The framework can be used for a wide variety of purposes, ranging from rich user interfaces to intense graphics editing.\n" + 
					"\n" + 
					"I'm distributing this degrafa swc and without the written permission of the degrafa team.  While it's not really required, it's recommended to\n" + 
					" keep your application from looking fugly.  Go visit them at: http://www.degrafa.org/  And please don't sue me.\n\n"+
					"Cairngorm is the lightweight micro-architecture for Rich Internet Applications built in Flex or AIR \n" + 
					"I'm distributing this cairngorm swc and without the written permission of the Cairngorm team.\n" + 
					"You can learn about Cairngorm at: http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm  And again please don't sue me. ");
			f.close();	
			file = File.applicationDirectory.resolvePath('docs/Degrafa_Beta2_Flex3.swc');
			trace(file.nativePath)
			file.copyTo(File.userDirectory.resolvePath(_location+ '/'+_projectName+'/libs/Degrafa_Beta2_Flex3.swc'), true);
			
		}

		protected function addCairngorm():void
		{
			var file:File = File.applicationDirectory.resolvePath('docs/Cairngorm.swc');
			trace(file.nativePath)
			file.copyTo(File.userDirectory.resolvePath(_location+ '/'+_projectName+'/libs/Cairngorm.swc'), true);
			
		}

		
		protected function writeBinSWF():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/bin-debug/main.swf');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/main-app.txt"));
		}
		
		protected function writeBinXML():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+ '/'+_projectName+'/bin-debug/main-app.xml');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();				
			}
			myTextLoader.load(new URLRequest("docs/main_app.txt"));
		}

		protected function writeASProperties():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+'/'+_projectName+'/.actionScriptProperties');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();	
			}
			
			myTextLoader.load(new URLRequest("docs/actionScriptProperties.txt"));
		}

		protected function writeFlexProperties():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+'/'+_projectName+'/.flexProperties');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();	
			}
			myTextLoader.load(new URLRequest("docs/flexProperties.txt"));
		}
		
		
		protected function writeProjectFile():void
		{
			var myTextLoader:URLLoader = new URLLoader();

			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
			var t:String
			function onLoaded(e:Event):void {
				t = e.target.data;
				var file:File = File.userDirectory.resolvePath(_location+'/'+_projectName+'/.project');
				var f:FileStream = new FileStream();
				f.open(file, FileMode.WRITE);
				f.writeUTFBytes(t);
				f.close();	
			}
			myTextLoader.load(new URLRequest("docs/project.txt"));
		}

	}
}